#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A股股票查询分析器安装和测试脚本
"""

import subprocess
import sys
import os

def install_requirements():
    """安装依赖包"""
    print("正在安装依赖包...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
        print("依赖包安装成功！")
        return True
    except subprocess.CalledProcessError as e:
        print(f"依赖包安装失败: {e}")
        return False

def test_imports():
    """测试导入是否成功"""
    print("测试导入模块...")
    try:
        import akshare as ak
        import pandas as pd
        import numpy as np
        print("所有模块导入成功！")
        return True
    except ImportError as e:
        print(f"模块导入失败: {e}")
        return False

def test_akshare_connection():
    """测试akshare连接"""
    print("测试akshare数据连接...")
    try:
        import akshare as ak
        # 尝试获取少量数据测试连接
        test_data = ak.stock_info_a_code_name()
        if not test_data.empty:
            print(f"数据连接成功！获取到 {len(test_data)} 只股票信息")
            return True
        else:
            print("数据连接失败：返回空数据")
            return False
    except Exception as e:
        print(f"数据连接测试失败: {e}")
        return False

def main():
    """主函数"""
    print("A股股票查询分析器 - 安装和测试")
    print("=" * 50)
    
    # 检查Python版本
    python_version = sys.version_info
    if python_version.major < 3 or (python_version.major == 3 and python_version.minor < 7):
        print(f"警告：当前Python版本为 {python_version.major}.{python_version.minor}，推荐使用Python 3.7+")
    
    # 1. 安装依赖
    if not install_requirements():
        print("安装失败，请手动安装依赖包")
        return
    
    # 2. 测试导入
    if not test_imports():
        print("模块导入测试失败，请检查安装")
        return
    
    # 3. 测试数据连接
    if not test_akshare_connection():
        print("数据连接测试失败，但程序仍可运行")
        print("可能的原因：网络连接问题或数据源暂时不可用")
    
    print("\n" + "=" * 50)
    print("安装完成！您现在可以运行以下命令：")
    print("1. python stock_analyzer.py    # 运行主程序")
    print("2. python example_usage.py     # 运行使用示例")
    print("=" * 50)

if __name__ == "__main__":
    main() 